/**
 * 
 */
package com.mywind.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.mywind.constant.Constant;
import com.mywind.entity.core.ResponseObj;
import com.mywind.entity.windfarm.WindFarmEntity;
import com.mywind.service.windfarm.IDiagnoseServ;

/**
 * @author a01513
 *
 */
@Controller
@SessionAttributes(value = "mysession")
@RequestMapping(value = "diagnose")
public class DiagnoseController {
	
	@Autowired
	private IDiagnoseServ service;
	
	@RequestMapping(value = "/toSearch.do")
	public String toSearch(){
		return "diagnose/search";
	}
	
	@RequestMapping(value = "/toETL.do")
	public String toETL(){
		return "diagnose/etl";
	}
	
	@RequestMapping(value = "/getWindFarmList.do")
	@ResponseBody
	public ResponseObj getWindFarmList(){
		
		ResponseObj obj = new ResponseObj();
		obj.setResBody(service.getAllWindFarm());
		
		return obj;
	}
	
	@RequestMapping(value = "/getTurbineList.do")
	@ResponseBody
	public ResponseObj getTurbineList(@Param("farmid") String farmid){
		ResponseObj obj = new ResponseObj();
		if(!"".equals(farmid))
			obj.setResBody(service.getTurbineList(farmid));
		return obj;
	}
	
	@RequestMapping(value = "/extractData.do")
	@ResponseBody
	public ResponseObj extractData(@Param("farmid") String farmid){
		ResponseObj obj = new ResponseObj();
		WindFarmEntity wfe = service.getWindFarmByFarmid(farmid);
		if(wfe.getEtl() == 0){
			service.setETLStatus(farmid, 1);
			obj.setMessage("running");
		}else{
			obj.setMessage("running");
		}
		return obj;
	}
	
	@RequestMapping(value = "/datagrid.do")
	@ResponseBody
	public ResponseObj datagrid(@Param("farmid") String farmid, @Param("turbine") String turbine, @Param("startDate") String startDate, @Param("endDate") String endDate){
		System.out.println(farmid + "," + turbine + "," + startDate + "," + endDate);
		int wcode = 0;
		if(!"".equals(turbine)){
			boolean isNan = Double.isNaN(Double.parseDouble(turbine));
			if(!isNan)
				wcode = Integer.parseInt(turbine);
		}
		ResponseObj obj = new ResponseObj();
		obj.setResBody(service.getDrecordByFTD(farmid, wcode, startDate, endDate));
		obj.setStatus(Constant.SUC);
		return obj;
	}
	
}
